package com.wjm.defusecamera;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import java.util.Iterator;
import java.util.List;

public class PermissionUtils {


    public static void appendStr(StringBuilder sb, String text) {
        if (!sb.toString().contains(text)) {
            sb.append(text).append("、");
        }
    }

    public static String getPermissionStr(List<String> permissionList) {
        Iterator<String> it = permissionList.iterator();
        Log.d("TAG", permissionList.toString());
        StringBuilder sb = new StringBuilder();
        String temp;
        while (it.hasNext()) {
            String next = it.next();
            if (next.contains("CAMERA")) {
                appendStr(sb, "相机");
            } else if (next.contains("RECORD_AUDIO")) {
                appendStr(sb, "录音");
            } else if (next.contains("READ_EXTERNAL_STORAGE") || next.contains("WRITE_EXTERNAL_STORAGE")) {
                appendStr(sb, "读写手机存储");
            } else if (next.contains("WRITE_CONTACTS") || next.contains("GET_ACCOUNTS") || next.contains("READ_CONTACTS")) {
                appendStr(sb, "修改联系人");
            } else if (next.contains("READ_CALL_LOG") || next.contains("READ_PHONE_STATE") || next.contains("CALL_PHONE") || next.contains("WRITE_CALL_LOG") || next.contains("USE_SIP") || next.contains("PROCESS_OUTGOING_CALLS") || next.contains("ADD_VOICEMAIL")) {
                appendStr(sb, "读取通话记录");
            } else if (next.contains("READ_CALENDAR") || next.contains("WRITE_CALENDAR")) {
                appendStr(sb, "日历");
            } else if (next.contains("ACCESS_FINE_LOCATION") || next.contains("ACCESS_COARSE_LOCATION")) {
                appendStr(sb, "定位");
            }
            /*else if (next.contains("BODY_SENSORS")) {
                appendStr(sb, "传感器");
            }*/
            /*else if (next.contains("READ_SMS") || next.contains("RECEIVE_WAP_PUSH") || next.contains("RECEIVE_MMS") || next.contains("RECEIVE_SMS") || next.contains("SEND_SMS") || next.contains("READ_CELL_BROADCASTS")) {
                appendStr(sb, "短信");
            }*/
            /*   else {
                sb.append(next);
            }*/
        }
        Log.d("TAG", sb.toString());
        String sbStr = sb.substring(0, sb.length() - 1);
        return sbStr;
    }

    /**
     * 打开应用权限设置界面
     * 参考：https://blog.csdn.net/luckrr/article/details/78211465
     * 另：https://www.jianshu.com/p/58c6ca8173c4（Android 跳转权限设置界面的终极适配(适配各大定制 ROM) - 简书）
     *
     * @param context
     */
    public static void startSettActivity(Context context) {
        String sdk = android.os.Build.VERSION.SDK; // SDK号

        String model = android.os.Build.MODEL; // 手机型号

        String release = android.os.Build.VERSION.RELEASE; // android系统版本号
        String brand = Build.BRAND;//手机厂商
        if (TextUtils.equals(brand.toLowerCase(), "redmi") || TextUtils.equals(brand.toLowerCase(), "xiaomi")) {
            gotoMiuiPermission(context);//小米
        } else if (TextUtils.equals(brand.toLowerCase(), "meizu")) {
            gotoMeizuPermission(context);
        } else if (TextUtils.equals(brand.toLowerCase(), "huawei") || TextUtils.equals(brand.toLowerCase(), "honor")) {
            gotoHuaweiPermission(context);
        } else {
            context.startActivity(getAppDetailSettingIntent(context));
        }
    }


    /**
     * 跳转到miui的权限管理页面
     */
    public static void gotoMiuiPermission(Context context) {
        try { // MIUI 8
            Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
            localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
            localIntent.putExtra("extra_pkgname", context.getPackageName());
            context.startActivity(localIntent);
        } catch (Exception e) {
            try { // MIUI 5/6/7
                Intent localIntent = new Intent("miui.intent.action.APP_PERM_EDITOR");
                localIntent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
                localIntent.putExtra("extra_pkgname", context.getPackageName());
                context.startActivity(localIntent);
            } catch (Exception e1) { // 否则跳转到应用详情
                context.startActivity(getAppDetailSettingIntent(context));
            }
        }
    }

    /**
     * 跳转到魅族的权限管理系统
     */
    public static void gotoMeizuPermission(Context context) {
        try {
            Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            context.startActivity(getAppDetailSettingIntent(context));
        }
    }

    /**
     * 华为的权限管理页面
     */
    public static void gotoHuaweiPermission(Context context) {
        try {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
            intent.setComponent(comp);
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
            context.startActivity(getAppDetailSettingIntent(context));
        }

    }

    /**
     * 获取应用详情页面intent（如果找不到要跳转的界面，也可以先把用户引导到系统设置页面）
     *
     * @return
     */
    public static Intent getAppDetailSettingIntent(Context context) {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", context.getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", context.getPackageName());
        }
        return localIntent;
    }
}
